<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf"%>

<%@ page session="false" %>

<html>
<head>
	<title>persons</title>
	
	<script type="text/javascript">
		function clearForm(form){
			var frm_elements = form.elements
			for (i = 0; i < frm_elements.length; i++){
				
				if(frm_elements[i].type){
					
					field_type = frm_elements[i].type.toLowerCase();
					
					switch (field_type){
						case "text":
							frm_elements[i].value = "";
							break;
						case "checkbox":
							frm_elements[i].checked = false;
							break;
						case "select-one":
						case "select-multi":
							frm_elements[i].selectedIndex = 0;
							break;
						default:
							break;
					}
					
				}

			}
		}
	</script>
	
</head>
<body>
<h1>
	Persons
</h1>
	
	<s:url value="/person/new" var="newPerson_url"/>
	<sf:form method="POST" modelAttribute="personFilter" action="${personFilter_url}">
	<fieldset>
		<table cellspacing="0">
			<tr>
				<td>
					<label for="person_name">Name:</label>
					<sf:input path="name" size="15" id="person_name"/>
				</td>
				<td>
					<label for="person_city">City:</label>
					<sf:select path="city.id"> 
						<sf:option value="" label="Select City" /> 
						<sf:options items="${cityList}" itemValue="id" itemLabel="name" /> 
					</sf:select>
				</td>
				<td>
					<label for="philosopher">Philosophers only:</label>
					<sf:checkbox path="philosopher" id="philosopher"/>
				</td>
				<!--td>
					<label for="both">Both:</label>
					<input type="checkbox" name="both" id="both"/>
				</td-->
				<td>
					<input name="search" type="submit" value="Search"/>
				</td>
				<td>
					<input type="button" name="show_all" value="Show All" onclick="clearForm(this.form);this.form.submit();">
				</td>
			</tr>
		</table>
	</fieldset>
	</sf:form>

<p>
	<div>
		<s:url value="/person/new" var="newPerson_url"/>
		<a href="${newPerson_url}"> Add Person </a>
	</div>
	
</p>

<p> 
	<table>
		<tr>
			<th>id</th>
			<th>name</th>
			<th>city</th>
			<th>philosopher</th>
			<th></th>
			<th></th>
		</tr>
		<c:forEach var="person" items="${persons}">
		<tr>
			<td>${person.id}</td>
			<td>${person.name}</td>
			<td>${person.city.name}</td>
			<td align="center">
				<c:choose> 
					<c:when test="${person.philosopher == true}"> 
						Yes 
					</c:when> 
					<c:otherwise> 
						No 
					</c:otherwise> 
				</c:choose> 
			</td>
			<td>
				<a href="<s:url value="/person/edit/${person.id}"/>"> edit </a>
			</td>
			<td>
				<a href="<s:url value="/person/delete/${person.id}"/>" 
					onclick="return confirm('Are you sure?')"> delete </a>
			</td>
		</tr>
		</c:forEach>
	</table>
</p>

<p>
	<div>
		<s:url value="/city" var="cities_url"/>
		<a href="${cities_url}"> Cities </a>
	</div>
</p>


</body>
</html>
